package com.czg.jsonschema2lombokpojo.annotator;

import com.fasterxml.jackson.databind.JsonNode;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JEnumConstant;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JMethod;

/**
 * 一个注解器（实现复合schema），可用于将多个注解器组合在一起。
 */
public class CompositeAnnotator implements Annotator {

    final Annotator[] annotators;

    /**
     * 创建一个新的复合注解器，由一组给定的子注解器组成。
     *
     * @param annotators 当这个注解器被调用时将被调用的注解器。所提供的子注解器将按照它们在此参数列表中出现的顺序调用。
     */
    public CompositeAnnotator(Annotator... annotators) {
        this.annotators = annotators;
    }

    @Override
    public void typeInfo(JDefinedClass clazz, JsonNode node, boolean isEnum) {
        for (Annotator annotator : annotators) {
            annotator.typeInfo(clazz, node, isEnum);
        }
    }

    @Override
    public void propertyInclusion(JDefinedClass clazz, JsonNode schema) {
        for (Annotator annotator : annotators) {
            annotator.propertyInclusion(clazz, schema);
        }
    }

    @Override
    public void propertyField(JFieldVar field, JDefinedClass clazz, String propertyName, JsonNode propertyNode) {
        for (Annotator annotator : annotators) {
            annotator.propertyField(field, clazz, propertyName, propertyNode);
        }
    }


    @Override
    public void anyGetter(JMethod getter, JDefinedClass clazz) {
        for (Annotator annotator : annotators) {
            annotator.anyGetter(getter, clazz);
        }
    }

    @Override
    public void anySetter(JMethod setter, JDefinedClass clazz) {
        for (Annotator annotator : annotators) {
            annotator.anySetter(setter, clazz);
        }
    }

    @Override
    public void enumCreatorMethod(JDefinedClass _enum, JMethod creatorMethod) {
        for (Annotator annotator : annotators) {
            annotator.enumCreatorMethod(_enum, creatorMethod);
        }
    }

    @Override
    public void enumValueMethod(JDefinedClass _enum, JMethod valueMethod) {
        for (Annotator annotator : annotators) {
            annotator.enumValueMethod(_enum, valueMethod);
        }
    }

    @Override
    public void enumConstant(JDefinedClass _enum, JEnumConstant constant, String value) {
        for (Annotator annotator : annotators) {
            annotator.enumConstant(_enum, constant, value);
        }
    }

    @Override
    public boolean isAdditionalPropertiesSupported() {
        for (Annotator annotator : annotators) {
            if (!annotator.isAdditionalPropertiesSupported()) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean isPolymorphicDeserializationSupported(JsonNode node) {
        for (Annotator annotator : annotators) {
            if (!annotator.isPolymorphicDeserializationSupported(node)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void dateTimeField(JFieldVar field, JDefinedClass clazz, JsonNode propertyNode) {
        for (Annotator annotator : annotators) {
            annotator.dateTimeField(field, clazz, propertyNode);
        }
    }

    @Override
    public void dateField(JFieldVar field, JDefinedClass clazz, JsonNode propertyNode) {
        for (Annotator annotator : annotators) {
            annotator.dateField(field, clazz, propertyNode);
        }
    }

    @Override
    public void timeField(JFieldVar field, JDefinedClass clazz, JsonNode propertyNode) {
        for (Annotator annotator : annotators) {
            annotator.timeField(field, clazz, propertyNode);
        }
    }
}
